import Router from 'koa-router';
import { connectProducer, processOrQueueMessage, producer } from '../../config/kafkaConfig';
import Result from '../../vo/result';
import logger from '../../Logger/logger';
import { ResultCode } from '../../enums/resultCode';

const router = new Router({
  prefix: '/api/test'
});

router.get('/test_kafka', async ctx => {
  try {
    await connectProducer();
    await producer.send({
      topic: 'orderTopic',
      messages: [
        { value: 'Hello Kafka' }
      ]
    });
    ctx.body = Result.success('发送成功');
  } catch (err) {
    logger.error('发送失败', err);
    ctx.body = Result.fail(ResultCode.FAIL, err);
  } finally {
    await producer.disconnect();
  }
});

router.post('/test_post', async ctx => {
  const body: any = ctx.request.body;
  ctx.body = Result.success(body);
});

export default router.routes();